package com.zzy.jkb.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzy.jkb.api.CommonResult;
import com.zzy.jkb.entity.Notification;
import com.zzy.jkb.entity.NucleicAcid;
import com.zzy.jkb.entity.User;
import com.zzy.jkb.service.INotificationService;
import com.zzy.jkb.service.INucleicAcidService;
import com.zzy.jkb.service.IUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 核酸检测记录 前端控制器
 * </p>
 *
 * @author zzy
 * @since 2023-02-03
 */
@Controller
@RequestMapping("/jkb/nucleicAcid")
public class NucleicAcidController {


    @Autowired
    INucleicAcidService iNucleicAcidService;

    @Autowired
    IUserService iUserService;

    @Autowired
    INotificationService iNotificationService;


    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询扫码记录")
    public CommonResult<List<NucleicAcid>> list(@RequestParam String userId) {

        List<NucleicAcid> list = iNucleicAcidService.list(Wrappers.lambdaQuery(NucleicAcid.class).eq(NucleicAcid::getUserId, userId).orderByDesc(NucleicAcid::getCheckTime));


        return CommonResult.success(list);

    }


    @RequestMapping(path = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("核酸检测")
    public CommonResult save(@RequestBody NucleicAcid nucleicAcid) {


        nucleicAcid.setCreateBy("翔董");
        nucleicAcid.setCreateTime(LocalDateTime.now());
        nucleicAcid.setCheckOrg("机构二");
        nucleicAcid.setCheckTime(LocalDateTime.now());
        return CommonResult.autoCheck(iNucleicAcidService.save(nucleicAcid));

    }


    @Transactional
    @RequestMapping(path = "/update",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation("核酸检测结果")
    public CommonResult saveResult(@Validated @RequestBody NucleicAcid nucleicAcid) {

        nucleicAcid.setResultTime(LocalDateTime.now());
        //查出当前管下的所有userID
        List<NucleicAcid> list = iNucleicAcidService.list(Wrappers.lambdaQuery(NucleicAcid.class).eq(NucleicAcid::getPipeCode, nucleicAcid.getPipeCode()));
        List<String> userIds = list.stream().map(NucleicAcid::getUserId).collect(Collectors.toList());

        if (nucleicAcid != null && nucleicAcid.getCheckResult().equalsIgnoreCase("1")) {
           iNucleicAcidService.update(Wrappers.lambdaUpdate(NucleicAcid.class).eq(NucleicAcid::getPipeCode, nucleicAcid.getPipeCode()).set(NucleicAcid::getCheckResult,"1"));
          //修改此管下所有人状态
            iUserService.update(Wrappers.lambdaUpdate(User.class).in(User::getUuid,userIds).set(User::getCodeStatus,"2").set(User::getHealthStatus,"1"));

            //判断该人该类型弹窗是否已经存在
            int count = iNotificationService.count(Wrappers.lambdaQuery(Notification.class).eq(Notification::getUserId, nucleicAcid.getUserId()).eq(Notification::getPopType, "1"));
            if(count==0){
                Notification  notification= new Notification();
                notification.setCreateBy("翔哥");
                notification.setCreateTime(LocalDateTime.now());
                notification.setPopType("1");
                notification.setPopCootent("您处于集中隔离状态，需要集中隔离21天");
                notification.setStartTime(LocalDateTime.now());
                notification.setDisable("1");//1为禁止关闭
                notification.setInformationSource("核酸检测机构");
                notification.setUserId(nucleicAcid.getUserId());
                iNotificationService.save(notification);
            }
        }else{
            iNucleicAcidService.update(Wrappers.lambdaUpdate(NucleicAcid.class).eq(NucleicAcid::getPipeCode, nucleicAcid.getPipeCode()).set(NucleicAcid::getCheckResult,"0"));
            //修改此管下所有人状态
            iUserService.update(Wrappers.lambdaUpdate(User.class).in(User::getUuid,userIds).set(User::getCodeStatus,"0").set(User::getHealthStatus,"0"));
        }
        return CommonResult.autoCheck(true);

    }

}
